package org.dylan.web.tag.css;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.dylan.core.util.SystemUtil;
import org.dylan.web.tag.css.CSSModel.CSSTheme;
import org.dylan.web.util.ResponseUtil;
import org.dylan.web.util.WebConstants;

/**
 * 
 * @author dylan
 * 
 */
public class CSSTag extends TagSupport {
	private static final long serialVersionUID = 6049675725736615829L;
	private Map<String, CSSModel> cssMap;
	private String id;
	private String theme;
	private String defaultTheme;
	private String context;
	private String path;
	private boolean useContextPath = true;

	public CSSTag() {
		cssMap = CSSDefineParser.cssMap();
		defaultTheme = SystemUtil.defaultTheme();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public boolean isUseContextPath() {
		return useContextPath;
	}

	public void setUseContextPath(boolean useContextPath) {
		this.useContextPath = useContextPath;
	}

	public String getTheme() {
		if (StringUtils.isNotBlank(theme)) {
			return theme;
		} else {
			String sessoinTheme = (String) ((HttpServletRequest) pageContext
					.getRequest()).getSession().getAttribute(
					WebConstants.SESSION_ATTRIBUTE_THEME);
			if (StringUtils.isNotBlank(sessoinTheme)) {
				return sessoinTheme;
			} else {
				return defaultTheme;
			}
		}
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		if (StringUtils.isBlank(context)) {
			context = ((HttpServletRequest) pageContext.getRequest())
					.getContextPath();
		}
		if (StringUtils.isNotEmpty(path)) {
			writeCSSPaths(path);
		} else if (StringUtils.isNotBlank(id)) {
			String[] types = id.split(",");
			for (String type : types) {
				type = type.trim();
				writeDefinedCSS(type);
			}
		}
		return super.doEndTag();
	}

	/**
	 * 输出预定义的CSS样式
	 * 
	 * @param type
	 */
	private void writeDefinedCSS(String type) {
		if (cssMap.containsKey(type)) {
			CSSModel model = cssMap.get(type);
			String theme = getTheme();
			CSSTheme cssTheme = model.getTheme(theme);
			String cssPath = cssTheme == null ? null : cssTheme.getPath();
			writeCSSPaths(cssPath);
		}
	}

	/**
	 * 输出多个css文件的引用路径，多个文件之间以逗号分隔
	 * 
	 * @param cssPaths
	 */
	protected void writeCSSPaths(String cssPaths) {
		if (StringUtils.isNotBlank(cssPaths)) {
			String[] arr = cssPaths.split(",");
			for (String path : arr) {
				writeCSSPath(path);
			}
		}
	}

	/**
	 * 输出css文件引用
	 * 
	 * @param path
	 */
	private void writeCSSPath(String path) {
		if (StringUtils.isBlank(path)) {
			return;
		}
		if (!context.endsWith("/")) {
			context += "/";
		}
		path = path.trim();
		if (isUseContextPath()) {
			path = context + path;
		}
		ResponseUtil.writeCSS(pageContext, path);
	}
}
